package com.smile.frame.seckill.core.disruptor;

import com.lmax.disruptor.EventHandler;
import com.smile.frame.common.result.Result;
import com.smile.frame.seckill.service.SecondKillService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 消费者
 *
 * @author LGC
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class SecondKillEventConsumer implements EventHandler<SecondKillEvent> {

    private final SecondKillService secondKillService;

    @Override
    public void onEvent(SecondKillEvent seckillEvent, long seq, boolean bool) {
        try {
            boolean result = secondKillService.startSecondKillByLock(seckillEvent.getSeckillId(), seckillEvent.getUserId());
            if (result) {
                log.info("用户:{}{}", seckillEvent.getUserId(), "秒杀成功");
            }else{
                log.info("用户:{}{}", seckillEvent.getUserId(), "秒杀失败");
            }
        } catch (Exception e) {

        }
    }
}